
local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.GameObject(),"ReadyObj_Base");
local CommonOHOH = _G.CommonOHOH;
local Battle_Cfg=CommonOHOH.config.Chapter.chapter2level
local CallBackManager = CommonOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local gReadyTime=8 --开始倒计时

--一个战斗准备对象

function M.new(self, ...)
	return M(self, ...);
end;

function M.ctor(self,battleInst,readyCtrl)

    self.gReadyCtrl=readyCtrl --准备控制器
    
    self.ReadyTime=gReadyTime --准备剩余时间
    self.ReadyPlayers={} --房间里面的玩家
    self.ReadUpdateCon=nil --准备更新倒计时
    self.PlayerRemoveCon=nil --玩家离开到计时


    self.PlayerAddEvent=ConnectClass.new() --玩家进入
    self.PlayerRemoveEvent=ConnectClass.new() --玩家离开
    self.ReayTimeEvent=ConnectClass.new()-- 倒计时
    self.ClearReadyEvent=ConnectClass.new() -- 清楚准备

    self.Id=tonumber(battleInst.Name)
    self.Cfg=Battle_Cfg[self.Id] --章节配置
	M.super.ctor(self,battleInst);
    self:AddLinster()
end;


function M.dtor(self)
	M.super.dtor(self);
end;

-- 添加碰撞检测
function M.AddLinster(self)

    local userId
    local enterTouch= self:GetEnterTouch()

    CallBackManager:AddRbxConnect(self,enterTouch.Touched, function(obj,chPart)
        local playerInst=_G.CommonOHOH.load.GeneralTool().IsPlayerTouch(chPart)
        if  not playerInst then
            return
        end
        self:AddPlayerRoom(playerInst)

    end);

    CallBackManager:AddRbxConnect(self,enterTouch.TouchEnded, function(obj,chPart)
        local playerInst=_G.CommonOHOH.load.GeneralTool().IsPlayerTouch(chPart)
        if  not playerInst then
            return
        end
        self:RemovePlayerRoom(playerInst)

    end);

end

 -- 清除准备信息
 function M.CleanReady(self)


    self.ReadyTime=gReadyTime --准备剩余时间
    self.ReadyPlayers={} --房间里面的玩家
    if  self.ReadUpdateCon then
        self.ReadUpdateCon:Disconnect()
    end

    if  self.PlayerRemoveCon then
        self.PlayerRemoveCon:Disconnect()
    end

    self.ReadUpdateCon=nil --准备更新倒计时
    self.PlayerRemoveCon=nil --玩家离开到计时

    self.ClearReadyEvent:Action()
end

--进入入口
function M.GetEnterPos(self)
    return self:WaitForChild_Loop("Enter.Touch").Position
end

-- 玩家加入准备
function M.AddPlayerRoom(self,playerInst)

    print("---------AddPlayerRoom------------",playerInst)
    if self.ReadyPlayers[playerInst] then
        return 
    end

    self.ReadyPlayers[playerInst]=true
    self.PlayerAddEvent:Action(playerInst)
    if  self.ReadUpdateCon then
        return 
    end

    self.PlayerRemoveCon= game.Players.PlayerRemoving:Connect(function(playerInst)
        self:RemovePlayerRoom(playerInst)
    end)
   self.ReadUpdateCon=self:AddStepUpdate(self.Ready,1)
  
end

-- 玩家离开准备
function M.RemovePlayerRoom(self,playerInst)

    if  self.ReadyPlayers[playerInst] then
        self.ReadyPlayers[playerInst]=nil
        self.PlayerRemoveEvent:Action(playerInst)
    end

end


--准备倒计时
function M.Ready(self,dt)
    if self:GetReadyCount()>0 then
        if  self.ReadyTime<=0 then
            self:CreateBattle()
            self:CleanReady()
            return true
        else
            self.ReadyTime= self.ReadyTime-1
            self.ReayTimeEvent:Action(self.ReadyTime)
        end
    else
        self:CleanReady()
        return true
    end
end;


--获取准备中的所有玩家
function M.GetAllReadyPlayers(self)
    local list={}
    for playerInst,v in pairs(self.ReadyPlayers) do
        list[playerInst]=v
    end
    return list
 end
 
 
 -- 获取准备人数
function M.GetReadyCount(self)
    local sum=0
    for k,v in pairs(self.ReadyPlayers) do
        sum=sum+1
    end
    return sum
end

-- 获取战斗
function M.GetBattleInst(self)
    return self.main_inst
end


-- 获取战斗ID
function M.GetId(self)
    return  self.Id
end


-- 获取战斗信息
function M.BattleData(self)
    return {
        battleInst=self.main_inst,
        levelIdList=self.Cfg.levels,
        battleId=self:GetId(),
        mapPerfab=self:GetMapPerfab(),
        playerList=self:GetAllReadyPlayers()
    }
end


-- 获取入口碰撞体
function M.GetEnterTouch(self)
    return  self:WaitForChild_Loop("Enter.Touch")
 end

 -- 获取退出位置
function M.GetExitCFrame(self)
    return  self:WaitForChild_Loop("Enter.Touch.exit").WorldCFrame
 end
 

 
 -- 获取BattleMap
 function M.GetMapPerfab(self)
     return self:WaitForChild_Loop("Battle")
  end
 
 -- 获取BattleMap
 function M.GetMapAtt(self)
    for _,v in pairs(self:WaitForChild_Loop("root"):GetChildren()) do
        if not v.using.Value then
            return v
        end
    end
 end

 
 -- 创建一场战斗
function M.CreateBattle(self)
    local data=self:BattleData()
    local map=data.mapPerfab:Clone()
    map:SetPrimaryPartCFrame(self:GetMapAtt().WorldCFrame)
 
    self:GetMapAtt().using.Value=true --将地图点位标记为使用中

    self:AddProcess(function()
        local battle=  CommonOHOH.load.Battle().new(map,data.levelIdList,data.playerList,data.battleId,data.battleInst)
        battle.EndEvent:Connect(function()
             self:GetMapAtt().using.Value=false --将地图点位标记为未使用
        end)
    end)

end

return M;
